package hjw.com.controller.adm;

import hjw.com.entity.Order;
import hjw.com.service.BookServiceImpl;
import hjw.com.service.OrderServiceImpl;
import hjw.com.util.PackJsn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Project:Academic - AdmOrderController
 *
 * @author IvanWei玮 -- maste
 * @version 1.0
 * @DataTime 2025/2/22 22:27
 * @since 1.8
 */
@RestController
@RequestMapping("/api/adm/order")
public class AdmOrderController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final OrderServiceImpl orderDao;
    private final BookServiceImpl bookDao;


    @Autowired
    public AdmOrderController(OrderServiceImpl orderDao, BookServiceImpl bookDao) {
        this.orderDao = orderDao;
        this.bookDao = bookDao;
    }

    @PostMapping("/findByParams")
    public String findByParams(@RequestParam Map<String, Object> map) {

        if (map == null || map.isEmpty()){
            return PackJsn.pack(203,"查询条件不能为空");
        }
        logger.info("查询订单信息："+map.toString());
        if (map.get("payStatus") != null && map.get("payStatus").toString().isEmpty()){
            map.remove("payStatus");
        }
        if (map.get("shipStatus") != null && map.get("shipStatus").toString().isEmpty()){
            map.remove("shipStatus");
        }
        if (map.get("ship2phone") != null && map.get("ship2phone").toString().isEmpty()){
            map.remove("ship2phone");
        }
        if (map.get("ship2name") != null && map.get("ship2name").toString().isEmpty()){
            map.remove("ship2name");
        }
        if (map.get("account") != null && map.get("account").toString().isEmpty()){
            map.remove("account");
        }
        if (map.get("lines") != null && map.get("lines").toString().equals("0")){
            map.remove("lines");
        }else {
            Integer lines = Integer.parseInt(map.get("lines").toString());
            map.put("lines",lines);
        }
        //默认查询语句从第一条开始
        map.put("curPage",0);

        List<Order> orders = orderDao.findByParams(map);

        if (orders.isEmpty()){
            return PackJsn.pack(203,"暂无订单信息");
        }
        Map<String,Object> result = new HashMap<>(2);
        result.put("orders",orders);
        result.put("total",orderDao.getOrderCount(map));
        for (Order order : orders) {
           logger.info("订单信息："+order.toString());
        }

        return PackJsn.pack(200,"订单信息",result);
    }

    @PostMapping("/shipGoods")
    public String updateOrder(@RequestBody Map<String,Object> map){
        logger.info("修改订单状态---->" + map);
        if (map == null || map.isEmpty()){
            return PackJsn.pack(203,"发货信息不能为空");
        }
        if (map.get("ordid") == null || map.get("ordid").toString().isEmpty()){
            return PackJsn.pack(203,"订单编号不能为空");
        }
        map.put("shipStatus",1);
        if (map.get("shipno") == null || map.get("shipno").toString().isEmpty()){
            return PackJsn.pack(203,"快递单号不能为空");
        }
        if (map.get("shipper") == null || map.get("shipper").toString().isEmpty()){
            return PackJsn.pack(203,"快递公司不能为空");
        }
        if (map.get("orderItem") == null || map.get("orderItem").toString().isEmpty()){
            return PackJsn.pack(203,"发货订单详情为空");
        }
        List<Map<String, Object>> orderItem = (List<Map<String, Object>>) map.get("orderItem");

        Map<String,Object> upBook = new HashMap<>(2);

        for (int i = 0,j=0; i < orderItem.size(); i++,j++) {
            //存储订单信息中取出的单条
            Map<String, Object> o = orderItem.get(i);
            //该条订单所对应的图书
            Map<String, Object> book = (Map<String, Object>) o.get("book");

            logger.info("发货书籍信息："+book);
            int bStock = Integer.parseInt(book.get("stock").toString());
            int num = Integer.parseInt(o.get("num").toString());
            // 事务处理技术补充模块
            if (bStock < num){
                for (int k = 0; k < j-1; k++) {
                    //存储订单信息中取出该条订单所对应的图书
                    Map<String, Object> b = (Map<String, Object>) orderItem.get(i).get("book");
                    //存储回推发送信息
                    Map<String,Object> outBook = new HashMap<>(2);
                    outBook.put("id",b.get("id"));
                    outBook.put("stock",b.get("stock"));
                    bookDao.update(outBook);
                }
                return PackJsn.pack(203,"书籍库存不足,无法发货");
            }
            int i1 = bStock - num;
            logger.info(book.get("id") + "书籍库存："+i1);
            //正式处理数据
            upBook.put("id",book.get("id"));
            upBook.put("stock",i1);
            bookDao.update(upBook);
        }
        int i = orderDao.updateOrder(map);

        if (i == 0) {
            return PackJsn.pack(203, "发货失败");
        }
        return PackJsn.pack(200, "发货成功");
    }


}
